﻿using MCMesServer.DBFunctions.TransformFuncs;
using MesBLL.EventService;
using MesDAL.DBInfo;
using MesDAL.DTO.ProcessSheet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.DBFunctions.DBFuncs
{
    public class ProcessSheetDBFunc
    {
        public async static Task<ProcessSheetDTO> AddProcessList(ProcessSheetDTO processSheetDTO)
        {
            using (var ser = new ProcessSheetService())
            {
                processsheet_log log = TransformDTOFunc.TransformProcessSheet2Model(processSheetDTO);
                processsheet_log addlog = await ser.AddAsync(log);
                ProcessSheetDTO processDTO = TransformDTOFunc.TransformModel2ProcessSheetDTO(addlog);
                if (processSheetDTO.workpiece.id == 0)
                {
                    using (var workSer = new WorkPieceInfoService())
                    {
                        workpiece_info info = await workSer.SelectWorkpieceByNumber(processSheetDTO.workpiece.number);
                        WorkpieceDTO dto = TransformDTOFunc.TransformModel2WorkPieceDTO(info);
                        processDTO.workpiece = dto;
                    }
                }
                else
                {
                    processDTO.workpiece = processSheetDTO.workpiece;
                }
                return processDTO;
            }
        }

        public async static Task<List<ProcessSheetDTO>> SelectProcessSheetListByWorkpiece(WorkpieceDTO workpiece)
        {
            using (var ser = new ProcessSheetService())
            {
                List<processsheet_log> logList = await ser.SelectLogListByWorkpieceId(workpiece.id);
                List<ProcessSheetDTO> list = new List<ProcessSheetDTO>();
                for (int i = 0; i < logList.Count; i++)
                {
                    ProcessSheetDTO dto = TransformDTOFunc.TransformModel2ProcessSheetDTO(logList[i]);
                    dto.workpiece = workpiece;
                    list.Add(dto);
                }
                return list;
            }
        }

        /// <summary>
        /// 检索未制作的生产计划单的进程单
        /// </summary>
        /// <returns></returns>
        public async static Task<List<ProcessSheetDTO>> SelectIncompleteProcessSheet()
        {
            using (var ser = new ProcessSheetService())
            {
                using (var workSer = new WorkPieceInfoService())
                {
                    List<processsheet_log> logList = await ser.SelectIncompleteProcessSheet();
                    List<ProcessSheetDTO> processList = new List<ProcessSheetDTO>();
                    for (int i = 0; i < logList.Count; i++)
                    {
                        ProcessSheetDTO dto = TransformDTOFunc.TransformModel2ProcessSheetDTO(logList[i]);
                        workpiece_info info = await workSer.SelectWorkpieceById(dto.workpiece.id);
                        if (info is null)
                        {
                            continue;
                        }
                        dto.workpiece = TransformDTOFunc.TransformModel2WorkPieceDTO(info);
                        processList.Add(dto);
                    }
                    return processList;
                }
            }
        }

        /// <summary>
        /// 整体编辑计划单
        /// </summary>
        /// <param name="processList"></param>
        internal async static void EditProcessSheetList(List<ProcessSheetDTO> processList)
        {
            using (var ser = new ProcessSheetService())
            {
                foreach (var item in processList)
                {
                    processsheet_log log = await ser.SelectLogByProcessSheetId(item.id);
                    if (log is null)
                    {
                        continue;
                    }
                    else
                    {
                        if (log.name != item.name || log.number != item.number || log.deadline != item.deadline || log.estimatedhours != item.estimatedhours)
                        {
                            log.name = item.name;
                            log.number = item.number;
                            log.deadline = item.deadline;
                            log.estimatedhours = item.estimatedhours;
                            await ser.EditAsync(log);
                        }
                    }
                }
            }
        }
    }
}
